import { Component, OnInit } from '@angular/core';
import { NzMessageService } from 'ng-zorro-antd/message';

import { ConfigShortAddressListService } from './address-list.service';
import { ConfigShortAddressGroupService } from './../address-group/address-group.service'
import {PermissionService} from '../../../../service/permission.service';
@Component({
  selector: 'app-config-short-address-list',
  templateUrl: './address-list.component.html',
  styleUrls: ['./address-list.component.scss']
})
export class ConfigShortAddressListComponent implements OnInit {

  /**
   * 模态框表单
   */
  modalForm: any = {
    // 跳转地址
    url: '',
    // 分组
    groupingId: '',
    // 简介
    intro: '',
    // 关键词
    keyword: '',
  }
  /**
   * 模态框显示 or 隐藏
   */
  isVisible: boolean = false;
  /**
   * tab表格数据 短地址
   */
  tableData: any = {
    records: []
  };

  /**
   * 分组列表数据
   */
  groupList: any = []
  /**
   * 查询字典参数
   */
  queryForm = {
    // 简介
    intro: "",
    // 分组 id
    groupingId: '',
    // 多少页，默认1
    page: 1,
    // 每页多少条，默认10
    pageSize: 10
  }
  /**
   * 全局 loading
   */
  messageId: string = "";

  constructor(
    private message: NzMessageService,
    private ConfigShortAddressService: ConfigShortAddressListService,
    private ConfigShortAddressGroupService: ConfigShortAddressGroupService,
    public permission: PermissionService,
  ) { }

  ngOnInit() {
    this.httpList();
    this.httpGroupList()
  }
  /**
   * 查询
   */
  onQuery() {
    this.queryForm.page = 1
    this.httpList();
  }
  /**
   * 重置
   */
  resetQuery() {
    this.queryForm.intro = '';
    this.queryForm.groupingId = '';
    this.queryForm.page = 1
    this.httpList();
  }
  /**
   * 页码改变
   * @param index 页码数
   */
  onPageIndexChange(index: number) {
    this.queryForm.page = index;
    this.httpList();
  }
  /**
   * 每页条数改变的回调
   * @param index 页码数
   */
  onPageSizeChange(index: number) {
    this.queryForm.pageSize = index;
    this.httpList();
  }

  /**
   * 新增 打开弹窗
   * @param id
   */
  onOpenAddModal() {
    this.modalForm = {}
    this.isVisible = true;
  }
  // 编辑 打开弹窗
  onTableEdit(item: any) {
    const { id, url, intro, groupingId } = item
    this.modalForm.id = id
    this.modalForm.url = url
    this.modalForm.intro = intro
    this.modalForm.groupingId = groupingId,
    this.isVisible = true;
  }
  // 删除
  onTableDelete(item: any) {
    this.httpDeleteAddress(item)
  }

  /**
   *  取消 关闭弹窗
   */
  onCancel() {
    this.isVisible = false;
  }
  /**
   * 新增 or 编辑 模态框提交
   * @param id
   */
  onConfirm() {
    const { url, groupingId, intro, id } = this.modalForm
    if (!groupingId) {
      this.message.warning('请输入分组')
      return
    }
    if (!url?.trim()) {
      this.message.warning('请输入跳转地址')
      return
    }
    if (!intro?.trim()) {
      this.message.warning('请输入简介')
      return
    }

    if (id) {
      this.httpEditAddress() // 编辑
    } else {
      this.httpSaveAddress() // 保存
    }
    this.isVisible = false;
  }
  /**
   * 查询地址
   */
  async httpList() {
    this.createBasicMessage()
    try {
      const { data }: any = await this.ConfigShortAddressService.getList(this.queryForm)
      this.removeBasicMessage()
      if (data) {
        this.tableData = data
      }
    } catch (error) {
      this.removeBasicMessage()
    }
  }

  /**
   * 查询地址
   */
  async httpGroupList() {
    const params = {
      page: 1,
      pageSize: 999,
    }
    try {
      const { data }: any = await this.ConfigShortAddressGroupService.getList(params)
      const { records } = data || {}
      if (records) {
        this.groupList = records
      }
    } catch (error) {
    }
  }

  /**
   * 保存地址
   */
  async httpSaveAddress() {
    this.createBasicMessage()
    try {
      const { code, message }: any = await this.ConfigShortAddressService.save(this.modalForm)
      this.removeBasicMessage()
      if (code == 0) {
        this.message.success('保存成功')
        this.queryForm.page = 1
        this.httpList();
        return
      }
      message && this.message.warning(message)
    } catch (error) {
      this.removeBasicMessage()
    }
  }

  /**
   * 编辑地址
   */
  async httpEditAddress() {
    this.createBasicMessage()
    try {
      let params = {...this.modalForm}
      for (let key in params) {
        if (params[key] == '' || params[key] == null) {
          delete params[key];
        }
      }
      
      const { code, message }: any = await this.ConfigShortAddressService.edit(params)
      this.removeBasicMessage()
      if (code == 0) {
        this.message.success('编辑成功')
        this.queryForm.page = 1
        this.httpList();
        return
      }
      message && this.message.warning(message)
    } catch (error) {
      this.removeBasicMessage()
    }
  }

  /**
   * 删除地址
   */
  async httpDeleteAddress(item: any) {
    const { id } = item
    if (!id) {
      return
    }
    const params = {
      id
    }
    this.createBasicMessage()
    try {
      const { code, message }: any = await this.ConfigShortAddressService.delete(params)
      this.removeBasicMessage()
      if (code == 0) {
        this.message.success('删除成功')
        this.queryForm.page = 1
        this.httpList();
        return
      }
      message && this.message.warning(message)
    } catch (error) {
      this.removeBasicMessage()
    }
  }

  /**
   * 开启loading
   */
  createBasicMessage(): void {
    this.messageId = this.message.loading('正在请求...', { nzDuration: 0 }).messageId;
  }
  /**
   * 移除loading
   */
  removeBasicMessage() {
    this.message.remove(this.messageId);
    this.messageId = "";
  }
  /**
   * 全局展示操作反馈信息
   * @param type 其他提示类型 success:成功 error:失败 warning：警告
   */
  createMessage(type: any, str: any): void {
    this.message.create(type, str);
  }
}
